package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Favorite;
import com.mall.db.service.FavoriteService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxd
 * @since 2021-09-27
 */
@RestController
@RequestMapping("/system/fac")
public class FavoriteController extends BaseController<Favorite> {

    @Resource(name = "favoriteService")
    private FavoriteService fas;

    @Override
    public Map<String, Object> find(Favorite favorite) {
        PageHelper.startPage(favorite.getPage(),favorite.getLimit());
        QueryWrapper<Favorite> qw =new QueryWrapper<>();

        if (!StringUtils.isEmpty(favorite.getgId())){
            qw.eq("g_id",favorite.getgId());
        }

        if (!StringUtils.isEmpty(favorite.getmId())){
            qw.eq("m_id",favorite.getmId());
        }

        List<Favorite> list=fas.list(qw);
        PageInfo<Favorite> page=new PageInfo<>(list,favorite.getLimit());
        return super.findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> add(Favorite favorite) {
        favorite.setCreateTime((new Date()));
        boolean b = fas.save(favorite);
        return this.iud("新增",b,favorite);
    }

    @Override
    public Map<String, Object> delete(Favorite favorite) {
        Map<String,Object>map =new HashMap<>();
        if (!StringUtils.isEmpty(favorite.getId())){
            fas.removeById(favorite.getId());
            map.put("is",true);
            map.put("msg","取消收藏成功");
        }else if (!StringUtils.isEmpty(favorite.getgId())  &&  !StringUtils.isEmpty(favorite.getmId())){
            QueryWrapper<Favorite> qw = new QueryWrapper<>();
            qw.eq("g_id",favorite.getgId());
            qw.eq("m_id",favorite.getmId());
            boolean b = fas.remove(qw);
            map.put("is",b);
            map.put("msg",b?"取消收藏成功":"取消收藏失败");
        }else {
            map.put("is",false);
            map.put("msg","取消失败");
        }
        return map;
    }
}

